/*
 * Wazuh Vulnerability scanner - Scan Orchestrator
 * Copyright (C) 2015, Wazuh Inc.
 * April 22, 2024.
 *
 * This program is free software; you can redistribute it
 * and/or modify it under the terms of the GNU General Public
 * License (version 2) as published by the FSF - Free Software
 * Foundation.
 */

#ifndef _GLOBAL_SYNC_INVENTORY_HPP
#define _GLOBAL_SYNC_INVENTORY_HPP

#include "chainOfResponsability.hpp"
#include "indexerConnector.hpp"
#include "scanContext.hpp"

/**
 * @brief GlobalSyncInventory class.
 *
 * @tparam TIndexerConnector indexer connector type.
 * @tparam TScanContext scan context type.
 */
template<typename TIndexerConnector = IndexerConnector, typename TScanContext = ScanContext>
class TGlobalSyncInventory final : public AbstractHandler<std::shared_ptr<TScanContext>>
{
private:
    std::shared_ptr<TIndexerConnector> m_indexerConnector;

public:
    // LCOV_EXCL_START
    /**
     * @brief GlobalSyncInventory constructor.
     *
     * @param indexerConnector Indexer connector.
     */
    explicit TGlobalSyncInventory(std::shared_ptr<TIndexerConnector> indexerConnector)
        : m_indexerConnector(std::move(indexerConnector))
    {
    }
    // LCOV_EXCL_STOP

    /**
     * @brief Handles request and passes control to the next step of the chain.
     *
     * @param data Scan context.
     * @return std::shared_ptr<TScanContext> Abstract handler.
     */
    std::shared_ptr<TScanContext> handleRequest(std::shared_ptr<TScanContext> data) override
    {
        std::string key = data->agentId().compare("000") == 0 && data->clusterStatus()
                              ? std::string(data->clusterNodeName()) + "_"
                              : "";
        key.append(data->agentId());

        m_indexerConnector->sync(key);

        return AbstractHandler<std::shared_ptr<TScanContext>>::handleRequest(std::move(data));
    }
};

using GlobalSyncInventory = TGlobalSyncInventory<>;

#endif // _GLOBAL_SYNC_INVENTORY_HPP
